/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.ta.web;

import java.io.IOException;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.ta.entity.StAttachment;
import com.thinkgem.jeesite.modules.ta.entity.StDynamicResource;
import com.thinkgem.jeesite.modules.ta.entity.StUserSign;
import com.thinkgem.jeesite.modules.ta.service.StAttachmentService;

/**
 * 附件Controller
 * 
 * @author TTAO
 * @version 2017-06-05
 */
@Controller
@RequestMapping(value = "${adminPath}/ta/stAttachment")
public class StAttachmentController extends BaseController {

	@Autowired
	private StAttachmentService stAttachmentService;

	@ModelAttribute
	public StAttachment get(@RequestParam(required = false) String id) {
		StAttachment entity = null;
		if (StringUtils.isNotBlank(id)) {
			entity = stAttachmentService.get(id);
		}
		if (entity == null) {
			entity = new StAttachment();
		}
		return entity;
	}

	@RequiresPermissions("ta:stAttachment:view")
	@RequestMapping(value = { "list", "" })
	public String list(StAttachment stAttachment, HttpServletRequest request,
			HttpServletResponse response, Model model) {
		Page<StAttachment> page = stAttachmentService.findPage(
				new Page<StAttachment>(request, response), stAttachment);
		model.addAttribute("page", page);
		return "modules/ta/stAttachmentList";
	}

	@RequiresPermissions("ta:stAttachment:view")
	@RequestMapping(value = "form")
	public String form(StAttachment stAttachment, Model model) {
		model.addAttribute("stAttachment", stAttachment);
		return "modules/ta/stAttachmentForm";
	}

	/*
	 * @RequestMapping(value = "save") public String
	 * save(@RequestParam("DocContent") MultipartFile file, String userid, Model
	 * model, RedirectAttributes redirectAttributes) throws IOException { if
	 * (file == null) { return "redirect:" + Global.getAdminPath() +
	 * "/ta/stUserSign/index"; } StAttachment stAttachment = new StAttachment();
	 * byte[] bytes = file.getBytes(); stAttachment.setData(bytes);
	 * stAttachmentService.save(stAttachment); addMessage(redirectAttributes,
	 * "修改用户签名成功"); return "redirect:" + Global.getAdminPath() +
	 * "/ta/stUserSign/index"; }
	 */
	@RequestMapping(value = "dsoframer")
	public String dsoframer(String name, String aid) {
		return "modules/ta/dsoframer.jsp?name=" + name + "&aid=" + aid;
	}

	@RequestMapping(value = "save")
	public String save(@RequestParam("DocContent") MultipartFile file,
			String userid, Model model, RedirectAttributes redirectAttributes)
			throws IOException {
		if (file == null) {
			return "redirect:" + Global.getAdminPath() + "/ta/stUserSign/index";
		}
		StUserSign stUserSign = new StUserSign();
		stUserSign.setUserid(userid);
		stUserSign.setSavedate(new Date());
		byte[] bytes = file.getBytes();
		stUserSign.setSignpic(bytes);
		addMessage(redirectAttributes, "修改用户签名成功");
		return "redirect:" + Global.getAdminPath() + "/ta/stUserSign/index";
	}

	@RequestMapping(value = "open")
	public String open(String aid) {
		return "modules/ta/byte.jsp?aid=" + aid;
	}

	@RequiresPermissions("ta:stAttachment:edit")
	@RequestMapping(value = "delete")
	public String delete(StAttachment stAttachment,
			RedirectAttributes redirectAttributes) {
		stAttachmentService.delete(stAttachment);
		addMessage(redirectAttributes, "删除附件成功");
		return "redirect:" + Global.getAdminPath() + "/ta/stAttachment/?repage";
	}

}